package com.example.nsl;

import android.animation.Animator;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.widget.FrameLayout;

public class DragView extends View {



    private int mLastX;
    private int mLastY;
    public DragView(Context context) {
        this(context, null);

    }

    public DragView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DragView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    int lastJlX = 0, lastJlY = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                mLastX = x;
                mLastY = y;
                Log.e("onTouc DOWN","x:"+x + "  Y:"+y);
                break;
            case MotionEvent.ACTION_MOVE:
                int offsetX = x - mLastX;
                int offsetY = y - mLastY;

                lastJlX=lastJlX+offsetX;
                lastJlY=lastJlY+offsetY;

//                Log.e("onTouchEvent ACTION_MOVE","x:"+ x + "  Y:"+y);

                offsetLeftAndRight(offsetX);
                offsetTopAndBottom(offsetY);
//
//                setTranslationX(lastJlX);
//                setTranslationY(lastJlY);

//                setX(lastJlX);
//                setY(lastJlY);

//                setX(getX()+offsetX);
//                setY(getY()+offsetY);
//                setY(lastJlY);



//                layout(getLeft() + offsetX, getTop() + offsetY,getTop
//                        getRight() + offsetX, getBottom() + offsetY);
                break;
            case MotionEvent.ACTION_CANCEL :
            case MotionEvent.ACTION_UP:
                int[] offsetInWindow=new int[2];
                int[] offsetXInWindow=new int[2];
//                ((FrameLayout) getParent()).
                        getLocationInWindow(offsetInWindow);

                Log.e("onTouchEvent CU","x:"+ lastJlX + "  Y:"+lastJlY);

                Log.e("onTouchEvent CU","x:"+ offsetInWindow[0] + "  Y:"+offsetInWindow[1]);
//                ((FrameLayout) getParent()).
//                        getLocationOnScreen(offsetXInWindow);
//                Log.e("onTouchEvent CU","x:"+ offsetXInWindow[0] + "  Y:"+offsetXInWindow[1]);
                Log.e("onTouchEvent CU","x:"+ getLeft() + "  Y:"+getTop() );

//              WindowManager.LayoutParams lp=((MainActivity)getContext()).getWindow().getAttributes();
//                lp.width=800;
//                lp.height=800;
//                ((MainActivity)getContext()).getWindow().setAttributes(lp);
//                View view,
//                int centerX,  int centerY, float startRadius, float endRadius

//                Animator anim =
//                        ViewAnimationUtils.createCircularReveal(this,
//                        getWidth()/2,getWidth()/2,0,getWidth()
//                        );
//                anim.setDuration(1000);
//                setVisibility(View.VISIBLE);
//                anim.start();
                break;
        }
        return true;
    }
}